const {Article, User} = require('../model')

// 获取文章列表
exports.getArticles = async (req, res, next) => {
   try {
     const { limit = 3, offset = 0, author, tag } = req.query
     const filter = {}
     if(tag) {
       filter.tagList = tag
     }

     if(author) {
       const user = await  User.findOne({username:author})
       user&&(filter.author = user._id)
     }
     // TODO 收藏筛选
     const articles = await Article.find(filter)
      .limit(parseInt(limit))
      .skip(parseInt(offset))
      .sort({
        createdAt: -1 // -1 降序， 1升序
      })
     const articlesCount = await Article.countDocuments()
     res.status(200).json({articles, articlesCount})
   } catch (error) {
     next(error)
   }
 }


// 获取用户关注的作者文章列表
exports.getFeedArticles = (req, res, next) => {
  try {
    res.send('post /getFeedArticles')
  } catch (error) {
    next(error)
  }
}


// 获取文章
exports.getArticle = async (req, res, next) => {
  try {
    const article = await Article.findById(req.params.slug)
      .populate('author')
    if(!article) {
      return res.status(404).end()
    }
    res.status(200).json({article})
  } catch (error) {
    next(error)
  }
}


// 创建文章
exports.createArticle = async (req, res, next) => {
  try {
    const article = new Article(req.body.article)
    article.author = req.user._id
    article.populate('author').execPopulate()
    await article.save()
    
    res.status(201).json({article})
  } catch (error) {
    next(error)
  }
}


// 更新文章
exports.updateArticle = async (req, res, next) => {
  try {
    const article = req.article
    const {title,about,description,body} = req.body.article
    title&&(article.title = title)
    about&&(article.about = about)
    description&&(article.description = description)
    body&&(article.body = body)
    await article.save()
    res.status(201).json({article})
  } catch (error) {
    next(error)
  }
}


// 删除文章
exports.deleteArticle = async (req, res, next) => {
  try {
    const article = req.article
    await article.remove()
    res.status(204).end('删除成功')
  } catch (error) {
    next(error)
  }
}


// 添加文章评论
exports.createArticleComment = (req, res, next) => {
  try {
    res.send('post /createArticleComment')
  } catch (error) {
    next(error)
  }
}


// 获取评论列表
exports.getArticleComments = (req, res, next) => {
  try {
    res.send('post /getArticleComments')
  } catch (error) {
    next(error)
  }
}


// 删除文章品论
exports.deleteArticleComment = (req, res, next) => {
  try {
    res.send('post /deleteArticleComment')
  } catch (error) {
    next(error)
  }
}


// 添加收藏
exports.createArticleFav = (req, res, next) => {
  try {
    res.send('post /createArticleFav')
  } catch (error) {
    next(error)
  }
}

// 删除收藏
exports.deleteArticleFav = (req, res, next) => {
  try {
    res.send('post /deleteArticleFav')
  } catch (error) {
    next(error)
  }
}
// 添加标签
exports.createArticleTag = (req, res, next) => {
  try {
    res.send('post /createArticleTag')
  } catch (error) {
    next(error)
  }
}